package com.read.service.impl;

import com.read.dto.params.RegisterParamsDto;
import com.read.dto.result.JwtUserDto;
import com.read.mapper.UserMapper;
import com.read.service.UserService;
import com.read.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sh-sunb6
 * @description
 * @date 2019-02-19 9:05
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int registerUser(RegisterParamsDto paramsDto) {
        try {
            paramsDto.setPassword(EncryptUtil.encryptSHAWithSalt(paramsDto.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userMapper.registerUser(paramsDto);
    }

    @Override
    public boolean isUserNameUsable(String userName) {

        int i = userMapper.selectUserCountByUserName(userName);

        return (i == 0) ? true : false;
    }

    @Override
    public JwtUserDto loginUser(String account, String password) {
        try {
            password = EncryptUtil.encryptSHAWithSalt(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userMapper.loginUser(account, password);
    }
}
